// src/model/album/photos.js
const { Sequelize } = require("sequelize");
// const sequelize = require("@/db/seq");
const sequelize = require("@/db/seq");

const Photo = sequelize.define("blog_photo", {
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true,
  },
  albumId: {
    type: Sequelize.INTEGER,
    allowNull: false,
    comment: "关联的相册ID",
  },
  url: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: "图片地址",
  },
  name: {
    type: Sequelize.STRING,
    allowNull: false,
    comment: "图片名称",
  },
  user_id: {
    type: Sequelize.INTEGER,
    allowNull: false,
    comment: "上传用户ID",
  },
  description: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: "图片描述",
  },
  order: {
    type: Sequelize.INTEGER,
    allowNull: true,
    comment: "图片排序",
  },
  size: {
    type: Sequelize.INTEGER,
    allowNull: true,
    comment: "图片大小（字节）",
  },
  mime_type: {
    type: Sequelize.STRING,
    allowNull: true,
    comment: "图片MIME类型",
  },
  createdAt: {
    type: Sequelize.DATE,
    allowNull: false,
    defaultValue: Sequelize.NOW,
    comment: "创建时间",
  },
  updatedAt: {
    type: Sequelize.DATE,
    allowNull: false,
    defaultValue: Sequelize.NOW,
    comment: "更新时间",
  },
}, {
  freezeTableName: true,
});

// 同步模型到数据库
// Photo.sync({ alter: true })


Photo.associate = (models) => {
  Photo.belongsTo(models.blog_album, {
    foreignKey: 'albumId',
    as: 'album'
  });
  Photo.belongsTo(models.blog_user, {
    foreignKey: 'user_id',
    as: 'user'
  });
};
module.exports = Photo;